package chapter14

/**
 * author: yuhui
 * descriptions:
 * date: 2024 - 10 - 26 11:06 上午
 */

import scala.util.Random
import java.io.BufferedWriter
import java.io.FileWriter

object DataViewToFileBuffered {

  def main(args: Array[String]): Unit = {

    val user = Array("余辉", "辉哥", "段海涛", "涛哥", "刘备", "张飞", "关羽", "赵云", "马超")
    val date = Array("2025-01-01", "2025-01-02", "2025-01-03", "2025-01-04", "2025-01-05",
      "2025-01-06", "2025-01-07", "2025-01-08", "2025-01-09", "2025-01-10")
    val URL = Array("https://www.zhihu.com/", "https://www.baidu.com/", "https://cn.aliyun.com/", "https://www.sohu.com/",
      "https://www.163.com/", "https://www.taobao.com/", "https://www.bytedance.com/zh/", "https://www.didiglobal.com/",
      "https://www.douyin.com/", "https://www.kuaishou.com/new-reco"
    )

    val writer = new BufferedWriter(new FileWriter("doc/output_buffered.txt"))

    dataRun(writer, "2025-01-01", 1000, user, URL)
    dataRun(writer, "2025-01-02", 839, user.filter(tp => !tp.contains("哥")), URL)
    dataRun(writer, "2025-01-03", 956, user.filter(tp => !tp.contains("辉哥")), URL)
    dataRun(writer, "2025-01-04", 740, user.filter(tp => !tp.contains("涛哥")), URL)
    dataRun(writer, "2025-01-05", 960, user.filter(tp => !tp.contains("刘备")), URL)
    dataRun(writer, "2025-01-06", 1000, user.filter(tp => !tp.contains("张飞")), URL)
    dataRun(writer, "2025-01-07", 790, user, URL)
    dataRun(writer, "2025-01-08", 480, user.filter(tp => !tp.contains("关羽") && !tp.contains("马超")), URL)
    dataRun(writer, "2025-01-09", 690, user.filter(tp => !tp.contains("赵云") && !tp.contains("涛")), URL)
    dataRun(writer, "2025-01-10", 900, user.filter(tp => !tp.contains("马超") && !tp.contains("辉")), URL)

    writer.close()

  }

  def dataRun(writer: BufferedWriter, dt: String, number: Int, user: Array[String], URL: Array[String]) {
    // 使用 for 循环循环1000次
    for (i <- 0 until number) {
      // 在这里执行你想要的操作
      println(s"这是第 $i 次循环")
      // 注意：如果这里的操作很耗时，大量打印可能会使控制台输出变得非常慢

      var usernum = user(Random.nextInt(user.length))
      var url = URL(Random.nextInt(URL.length))

      println("时间", dt, "    用户", usernum, "    URL", url)

      val line = dt + "," + usernum + "," + url
      writer.write(line)
      writer.newLine()

    }
  }
}

